# -*- coding: utf-8 -*-
# filename: handle.py


import json
import time
import re
from lottery.view import mysql_util

def receive_bullet(request):
    try:
        conn = mysql_util.getconn()
        webData = json.loads(request.body)
        mobile = webData['mobile']
        message = webData['message']
        pattern1 = re.compile(r'1[0-9]{10}')
        if not message:
            results = {'result':False, 'msg':'弹幕消息不得为空'}
        elif pattern1.match(mobile):

            signs = db.select('sign',where="mobile='"+ mobile +"'")
            if len(signs) > 0 :
                message = str(message)
                message =  message.replace("&","")
                message =  message.replace(">","")
                message =  message.replace("<","")
                message =  message.replace("\"","")
                uptime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                db.insert('bullet',  mobile=mobile, uptime=uptime, message=message)
                results = {'result':True,'msg':'发送弹幕成功'}
            else:
                results = {'result':False, 'msg':'手机号未签到'}
        else:
            results = {'result':False, 'msg':'手机号错误'}
        return json.dumps(results)
    except Exception as e:
        return e.args
